package controller;

import controller.util.PaginationHelper;
import facade.FiledescriptionFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import manager.FileManager;
import manager.LogManager;

/**
 * BrowseController
 *
 * JSF Managed backing bean containing model showing logic for the browse page.
 * Also handes deletion of filedescriptions from the browse page for
 * administrators.
 */
@ManagedBean
@ViewScoped
public class BrowseController extends PaginationController implements Serializable {

    @EJB
    private FiledescriptionFacade fdFacade;
    @EJB
    private FileManager fileManager;
    @EJB
    private LogManager logManager;
    @ManagedProperty(value = "#{userController}")
    private UserController userController;

    public UserController getUserController() {
        return userController;
    }

    public void setUserController(UserController userController) {
        this.userController = userController;
    }
    private Integer cat;
    
    private String catS;

    public String getCatS() {
        return catS;
    }

    public void setCatS(String catS) {
        System.out.println("Got "+catS);
        this.catS = catS;
    }
    
    

    public Integer getCat() {
        return cat;
    }

    public void setCat(Integer cat) {
        this.cat = cat;
    }

    public void deleteFile(int id) {

        if (userController.getIsAdmin()) {
            fileManager.deleteFile(id);
            logManager.logInfo("browse", "User " + userController.getUser().getUsername()
                    + " (id " + userController.getUser().getId() + ") deleted file id " + id + ".");
        } else {
            logManager.logWarning("browse", "User " + userController.getUser().getUsername()
                    + " (id " + userController.getUser().getId() + ") tried to delete file id " + id + " but does not have authority.");
        }
    }

    public BrowseController() {
    }

    @Override
    public FiledescriptionFacade getFacade() {
        return fdFacade;
    }

    @Override
    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(15) {

                @Override
                public int getItemsCount() {
                    if (cat != null) {
                        return getFacade().countCategoryId(cat);
                    } else {
                        return getFacade().count();
                    }
                }

                @Override
                public DataModel createPageDataModel() {
                    int[] range = new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()};
                    if (cat != null) {
                        return new ListDataModel(getFacade().findByCategoryId(cat, range));
                    } else {
                        return new ListDataModel(getFacade().findRange(range));
                    }
                }
            };
        }
        return pagination;
    }
}
